local setting = {
    debug = "_debug_"
}

function setting:bind_setting(key, setting)
    self.content[key] = setting
end

function setting:initialize(path)
    self.path    = path
    self.content = {}
    setmetatable(self, {
        __index = self.content,
    })
end

function setting:load_setting()
    local config = json.load_file(self.path)
    if not config then
        return
    end

    for component_index, component_setting in pairs(self.content) do
        if config[component_index] then
            for key, _ in pairs(component_setting) do
                if config[component_index][key] ~= nil
                    and type(self.content[component_index][key]) == type(config[component_index][key])
                then
                    self.content[component_index][key] = config[component_index][key]
                end
            end
        end
    end
    self:save_setting()
end

function setting:save_setting()
    local debug = nil
    for index, content in pairs(self.content) do
        if self.debug == index then
            debug = content
            self.content[self.debug] = nil
            break
        end
    end

    json.dump_file(self.path, setting.content)

    if debug then
        self.content[self.debug] = debug
    end
end

return setting